const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const routerUser = require('./router/user');
const routerData = require('./router/data');

const port = process.env.SERVER_PORT || 3000;

const app = express();

app.use(cookieParser());

// 配置 CORS 中间件
app.use(cors({
  origin: process.env.VUE_URL || 'http://localhost:8080',
  credentials: true, // 允许携带凭证
}));

app.use(express.json());

app.use((req, res, next) => {
  console.log(`${new Date().toLocaleString()} : ${req.method} : ${req.url}`);
  next();
})

app.use('/api/user', routerUser);
// 为数据接口添加验证中间件
app.use('/api/data', require('./utils/verifyAuthToken'));
app.use('/api/data', routerData);

app.listen(port, () => {
  console.log(`\n\n\nServer is running at http://localhost:${port}`);
});
